document.addEventListener('DOMContentLoaded', function() {
    var sectionTitles = document.querySelectorAll('.section-title');

    sectionTitles.forEach(function(title) {
        title.addEventListener('click', function() {
            var content = this.nextElementSibling;
            var icon = this.querySelector('.toggle-icon');

            if (content.style.display === 'block') {
                content.style.display = 'none';
                icon.textContent = '▼';
            } else {
                content.style.display = 'block';
                icon.textContent = '▲';
            }
        });
    });

    // 登出按钮功能
    document.querySelector('.logout-btn').addEventListener('click', function() {
        window.location.href = '/admin_logout';
    });

    // 切换账号功能
    document.querySelector('.switch-account-btn').addEventListener('click', function() {
        window.location.href = '/admin_login';
    });

    // 显示当前时间
    function updateTime() {
        const now = new Date();
        const formattedTime = now.toLocaleString();
        document.getElementById('current-time').textContent = `当前时间: ${formattedTime}`;
    }
    setInterval(updateTime, 1000); // 每秒更新一次时间
    updateTime(); // 初始化显示时间

    var ctx = document.getElementById('salesChart').getContext('2d');
    var salesChart = new Chart(ctx, {
        type: 'line',
        data: {
            labels: JSON.parse(document.getElementById('dates-data').textContent),
            datasets: [{
                label: '每日销量',
                data: JSON.parse(document.getElementById('sales-data').textContent),
                borderColor: 'rgba(75, 192, 192, 1)',
                borderWidth: 1,
                fill: false
            }]
        },
        options: {
            scales: {
                x: {
                    type: 'time',
                    time: {
                        unit: 'day'
                    }
                },
                y: {
                    beginAtZero: true
                }
            }
        }
    });
});
